/**
 * Created by taohj on 2014/8/14.
 */


var dictionary = new Object();


/**
 * 模块列表
 * */
dictionary.initgrid = function () {


    dictionary.datagrid = $("#admin_dictionary_list_datagrid");


    dictionary.datagrid.datagrid({
        url: '/admin/dictionary/list',
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        sortName: "id",
        sortOrder: "asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,
        toolbar: [
            {
                iconCls: 'icon-add',
                text: '新增',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '新增',
                        href: '/admin/dictionary/createV',
                        modal: true,
                        width: 400
                    });


                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: '更新',
                handler: function () {
                    var selectRow = dictionary.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }
                    var id = "";
                    if (selectRow) {
                        id = selectRow.id;
                    }

                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '更新',
                        href: '/admin/dictionary/modifyV' + '?id=' + id,
                        modal: true,
                        width: 400
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '删除',
                handler: function () {

                    var selectRow = dictionary.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    $.messager.confirm('删除确认', '确定要删除此项么？ ', function (r) {
                        if (r) {
                            $.post('/admin/dictionary/remove', {
                                id: selectRow.id
                            }, function (result) {
                                util.requestUtil(result, null, dictionary.datagrid);
                            });
                        }
                    });
                    console.info(selectRow);
                }
            }
        ],
        columns: [
            [
                {field: "id", title: "编号", width: 60, sortable: true},
                {field: "name", title: "名称", width: 200, sortable: true},
                {field: "value", title: "值", width: 200, sortable: true},
                {field: "type", title: "类型", width: 200, sortable: true},
                {field: "description", title: "简介"}
            ]
        ]
    });
};


//
dictionary.authorizeInit = function (id) {


    dictionary.authorizeTreegrid = $("#sys_role_authorize_tree");

    dictionary.authorizeTreegrid.tree({
        url: functions.get("SysRoleAuthorizeList") + "?id=" + id,
        singleSelect: true,
        checkbox: true,
        fit: true,
        animate: true

    });


}


//新建提交
dictionary.createSubmit = function (button) {
    var form = util.form(button);
    form.form('submit',{
        url: '/admin/dictionary/create',
        success:function(result){
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionary.datagrid)
        }
    });

};


//修改提交
dictionary.modifySubmit = function (button) {
    var form = util.form(button);
    form.form('submit',{
        url: '/admin/dictionary/modify',
        success:function(result){
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionary.datagrid)
        }
    });
};


//搜索
dictionary.searchSubmit = function (button) {
    var form = util.form(button);
    var params = util.serializeObject(form);
    dictionary.datagrid.datagrid("load", params);
}

